﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using WebApplication1.Libs;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public string _SoLuong, _ThanhTien;

        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                if (!Page.IsPostBack)
                {
                    string dbName = lstDsDatabase.SelectedValue;
                    Connection.ConnectString(dbName);

                    cmbKyBaoCao.SelectedIndex = 17;
                    dateNgayBatDau.Enabled = true;
                    dateNgayKetThuc.Enabled = true;
                    DateTime now = DateTime.Now;
                    dateNgayBatDau.Date = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
                    dateNgayKetThuc.Date = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
                }
            }
            catch (Exception ex)
            {
                string display = ex.Message;
                ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "alert('" + display + "');", true);
            }
        }

        protected void cmbKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ThoiDiemBaoCao.ThietLapKhoangThoiGianBaoCao(cmbKyBaoCao.Text, cmbKyBaoCao, dateNgayBatDau, dateNgayKetThuc);
            }
            catch (Exception ex)
            {
                string display = ex.Message;
                ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "alert('" + display + "');", true);
            }
        }

        protected void btnThongKe_Click(object sender, EventArgs e)
        {
            DsThongKeBanLe ds = new DsThongKeBanLe();

            //Get database name form listbox (value)
            string dbName = lstDsDatabase.SelectedValue;

            string connectionString = Connection.ConnectString(dbName);
            SqlConnection con = new SqlConnection(connectionString);

            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                cmd.CommandTimeout = 1000 * 10;

                DateTime ngayBatDau = new DateTime();
                DateTime ngayKetThuc = new DateTime();
                ngayBatDau = dateNgayBatDau.Date;
                ngayKetThuc = dateNgayKetThuc.Date;

                if (chkShowDetail.Checked)
                {
                    cmd.CommandText = "Select_Detail_KhoanMucThongKeBanLe";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@UserID", null);
                    cmd.Parameters.AddWithValue("@MayTinhID", null);
                    cmd.Parameters.AddWithValue("@ThoiDiemDau", ngayBatDau);
                    cmd.Parameters.AddWithValue("@ThoiDiemCuoi", ngayKetThuc);

                    ds.Select_Detail_RutGon.Clear();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(ds.Select_Detail_RutGon);

                    //Thêm tổng ở đầu
                    object sumSoLuong, sumThanhTien;
                    sumSoLuong = ds.Select_Detail_RutGon.Compute("Sum(SoLuong)", "");
                    sumThanhTien = ds.Select_Detail_RutGon.Compute("Sum(ThanhTien)", "");

                    _SoLuong = String.Format("{0:### ### ### ###}", sumSoLuong);
                    _ThanhTien = String.Format("{0:### ### ### ###}", sumThanhTien);

                    //Binding to grid. Nếu tích vào chọn hiển thị chi tiết
                    gridResult.DataSource = ds.Select_Detail_RutGon;
                    gridResult.DataBind();
                }
                else
                {
                    DataTable dt = new DataTable();

                    cmd.CommandText = "SELECT SUM(C.VaLue1) SoLuong, SUM(C.Value6 * C.Value1) ThanhTien \n" +
                                    "FROM ChungTu A INNER JOIN LoaiChungTu LCT ON A.LoaiChungTuID = LCT.ID AND LCT.Ma IN ('HoaDonBanLe') \n" +
                                    "	INNER JOIN ChungTu B ON A.ChungTuID = B.ChungTuGocID \n" +
                                    "	INNER JOIN KhoanMucChungTu C ON C.ChungTuID = B.ChungTuID  \n" +
                                    "WHERE @ThoiDiemDau <= A.Ngay AND A.Ngay <=@ThoiDiemCuoi";

                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@ThoiDiemDau", ngayBatDau);
                    cmd.Parameters.AddWithValue("@ThoiDiemCuoi", ngayKetThuc);

                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    da.Fill(dt);

                    if (dt.Rows.Count >= 1)
                    {
                        _SoLuong = String.Format("{0:### ### ### ###}", dt.Rows[0]["SoLuong"]);
                        _ThanhTien = String.Format("{0:### ### ### ###}", dt.Rows[0]["ThanhTien"]);
                    }

                    gridResult.DataSource = null;
                    gridResult.DataBind();
                }
            }
            catch (Exception ex)
            {
                string display = ex.Message;
                ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "alert('" + display + "');", true);
            }
            finally
            {
                con.Close();
            }
        }

        protected void lstDsDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
            string dbName = lstDsDatabase.SelectedValue;
            Connection.ConnectString(dbName);

            gridResult.DataSource = null;
            gridResult.DataBind();
        }
    }
}
